package com.qinsoft.business.onsale.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.qinsoft.business.onsale.bean.OnSale;
import com.qinsoft.frame.messages.WriteMessage;
import com.qinsoft.frame.utils.DbUtil;

public class OnSaleDao
{

    public void save(OnSale onsale)
    {
        SqlSession sqlMap = null;
        try
        {
            Boolean flag = false;
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            flag = sqlMap.selectOne("com.qinsoft.business.onsale.sqlMap.onsale.exist", onsale.getNick());
            if(flag){
                //执行更新方法
                sqlMap.update("com.qinsoft.business.onsale.sqlMap.onsale.update",onsale);
            }
            else{
                //执行保存方法
                sqlMap.insert("com.qinsoft.business.onsale.sqlMap.onsale.insert", onsale);
            }
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".save(" + onsale.getNick() + ")", e);
        }
        finally
        {
            sqlMap.close();
        }
    }

    public OnSale select(String nick)
    {
        SqlSession sqlMap = null;
        OnSale onsale = new OnSale();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            //执行查询方法
            onsale = sqlMap.selectOne("com.qinsoft.business.onsale.sqlMap.onsale.select",nick);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".select("+ nick +")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return onsale;
    }

    public List<OnSale> selectAll(Integer begin)
    {
        SqlSession sqlMap = null;
        List<OnSale> list = new ArrayList<OnSale>();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            //执行查询方法
            list = sqlMap.selectList("com.qinsoft.business.onsale.sqlMap.onsale.selectAll",begin);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".selectAll("+ begin +")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return list;
    }
    
    public void change(Boolean state, String nick)
    {
        SqlSession sqlMap = null;
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("state", state);
            param.put("nick", nick);
            //执行更新方法
            sqlMap.update("com.qinsoft.business.onsale.sqlMap.onsale.change",param);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".change("+ nick +")", e);
        }
        finally
        {
            sqlMap.close();
        }
    }
}
